//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.yinhai.sso.web;

import org.jasig.cas.client.util.CommonUtils;
import org.jasig.cas.client.util.DESUtil;
import org.jasig.cas.client.util.NetUtils;
import org.jasig.cas.client.util.ServerUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.cas.ServiceProperties;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.util.Assert;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

public class TaCasAuthenticationEntryPoint implements AuthenticationEntryPoint, InitializingBean {
    private ServiceProperties serviceProperties;
    private String loginUrl;
    /** @deprecated */
    @Deprecated
    private boolean encodeServiceUrlWithSessionId = true;

    public TaCasAuthenticationEntryPoint() {
        //
    }

    public void afterPropertiesSet() throws Exception {
        Assert.hasLength(this.loginUrl, "loginUrl must be specified");
        Assert.notNull(this.serviceProperties, "serviceProperties must be specified");
    }

    public final void commence(HttpServletRequest servletRequest, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException {
        final String urlEncodedService = createServiceUrl(servletRequest, response);
        String redirectUrl = createRedirectUrl(urlEncodedService);

        preCommence(servletRequest, response);
        String ip = NetUtils.getLocalHost();
        int port = ServerUtils.getPort();
        String stService = "http://" + ip + ":" + port + servletRequest.getContextPath() + "/";
        try {
            stService = DESUtil.encrypt(stService, "toi3wfexhms=");
            if(redirectUrl.indexOf('?') > 0){
                redirectUrl += "&stService=" + URLEncoder.encode(stService, "UTF-8");
            }else{
                redirectUrl += "?stService=" + URLEncoder.encode(stService, "UTF-8");
            }
        } catch (Exception e) {

        }

        response.sendRedirect(redirectUrl);
    }

    protected String createServiceUrl(HttpServletRequest request, HttpServletResponse response) {
        return CommonUtils.constructServiceUrl((HttpServletRequest)null, response, this.serviceProperties.getService(), (String)null, this.serviceProperties.getArtifactParameter(), this.encodeServiceUrlWithSessionId);
    }

    protected String createRedirectUrl(String serviceUrl) {
        return CommonUtils.constructRedirectUrl(this.loginUrl, this.serviceProperties.getServiceParameter(), serviceUrl, this.serviceProperties.isSendRenew(), false);
    }

    protected void preCommence(HttpServletRequest request, HttpServletResponse response) {
        //
    }

    public final String getLoginUrl() {
        return this.loginUrl;
    }

    public final ServiceProperties getServiceProperties() {
        return this.serviceProperties;
    }

    public final void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    public final void setServiceProperties(ServiceProperties serviceProperties) {
        this.serviceProperties = serviceProperties;
    }

    /** @deprecated */
    @Deprecated
    public final void setEncodeServiceUrlWithSessionId(boolean encodeServiceUrlWithSessionId) {
        this.encodeServiceUrlWithSessionId = encodeServiceUrlWithSessionId;
    }

    /** @deprecated */
    @Deprecated
    protected boolean getEncodeServiceUrlWithSessionId() {
        return this.encodeServiceUrlWithSessionId;
    }
}
